<?php



namespace app\userapi\controller;

use app\service\user\UserService;
use app\userapi\controller\Base;
use support\Cache;

class Account extends Base
{
    protected $noLoginAction = ['registerConfig', 'register', 'login', 'sendEmailCode', 'getCaptcha'];

    /**
     * 用户注册
     */
    public function register(UserService $UserService)
    {
        $UserService->register();
        $this->success("注册成功");
    }

    /**
     * 用户登录
     */
    public function login(UserService $UserService)
    {
        $user = $UserService->login();
        $this->success('登录成功', $user);
    }

    /**
     * 发送邮箱验证码
     */
    public function sendEmailCode(UserService $UserService)
    {
        $UserService->sendEmailCode();
        $this->success("已发送验证码到你的邮箱，请注意查收！");
    }

    // userDetail
    public function userDetail()
    {
        $field = ['id', 'username', 'status', 'email', 'nickname', 'lastlogintime', 'is_freeze', 'create_time', 'avatar', 'vip_expire_time', 'points'];
        $user  = $this->user->visible($field)->toArray();

        // 是否是vip判断
        if ($user['vip_expire_time'] && $user['vip_expire_time'] > date('Y-m-d H:i:s')) {
            $user['is_vip'] = 1;
        } else {
            $user['is_vip'] = 0;
        }

        $data = [
            'user'  => $user,
            'perms' => '*'
        ];

        $this->success('获取成功', $data);
    }

    /**
     * 修改密码
     */
    public function changePassword()
    {
        // 验证邮箱是否存在
        if (!$this->user->email) {
            $this->error("请先绑定邮箱");
        }

        $code        = inputs('code/s', '');
        $newPassword = inputs('new_password/s', '');

        if (!$code || !$newPassword) {
            $this->error('参数错误');
        }

        // 验证邮箱验证码
        $cacheCode = Cache::get('email_code_' . $this->user->email);
        if (!$cacheCode || $cacheCode != $code) {
            $this->error("验证码错误");
        }

        // 清除验证码缓存
        Cache::delete('email_code_' . $this->user->email);

        // 更新密码
        $this->user->password = password_hash($newPassword, PASSWORD_DEFAULT);
        $this->user->save();

        $this->success("密码修改成功");
    }

    /**
     * 获取图形验证码
     */
    public function getCaptcha(UserService $UserService)
    {
        $captcha = $UserService->getCaptcha();
        $this->success('获取成功', $captcha);
    }

    /**
     * 更新用户头像
     */
    public function updateUserAvatar()
    {
        $file = request()->file('file');
        if (!$file) {
            $this->error('请上传头像');
        }

        if ($file && $file->isValid()) {
            $file_uri = '/avatar/' . $file->getUploadName();
            $file->move(public_path() . '/upload' . $file_uri);

            $avatar = conf('site_domain') . '/upload' . $file_uri;

            // 更新用户头像
            $this->user->avatar = $avatar;
            $this->user->save();

            $this->success('头像更新成功', ['avatar' => $avatar]);
        } else {
            $this->error('头像上传失败');
        }
    }

    /**
     * 修改用户昵称
     */
    public function updateNickname()
    {
        $nickname = inputs('nickname/s', '');

        if (!$nickname) {
            $this->error('请输入昵称');
        }

        // 更新昵称
        $this->user->nickname = $nickname;
        $this->user->save();

        $this->success('昵称修改成功');
    }

    /**
     * 绑定邮箱
     * 1.通过 getCaptcha获取图形验证码 验证通过后
     * 2.通过 sendEmailCode发送邮箱验证码
     * 3.通过 bindEmail绑定邮箱
     */
    public function bindEmail()
    {
        $email = inputs('email/s', '');
        $code  = inputs('code/s', '');

        if (!$email || !$code) {
            $this->error('参数错误');
        }

        // 验证邮箱格式
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $this->error('邮箱格式不正确');
        }

        // 验证邮箱是否已被使用
        $existUser = \app\common\model\User::where('email', $email)
            ->where('id', '<>', $this->user->id)
            ->find();

        if ($existUser) {
            $this->error('该邮箱已被使用');
        }

        // 验证邮箱验证码
        $cacheCode = Cache::get('email_code_' . $email);
        if (!$cacheCode || $cacheCode != $code) {
            $this->error("验证码错误");
        }

        // 清除验证码缓存
        Cache::delete('email_code_' . $email);

        // 更新邮箱
        $this->user->email = $email;
        $this->user->save();

        $this->success('邮箱绑定成功');
    }
}
